package second;


import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author
 * @since 2017/7/6 下午8:03
 * Purpose
 */
public class AsyncObservable {

    public static Observable asyncObservable() {
        return Observable.create(aSubscriber->{
            new Runnable(){

                @Override
                public void run() {
                    for(int i=0;i<50;i++) {
                        aSubscriber.onNext("value:"+i);
                    }
                    System.out.println("runnable"+Thread.currentThread());
                }
            }.run();
            System.out.println("observable:"+Thread.currentThread());
            aSubscriber.onComplete();
        });
    }

    public static Observable asyncNonblockingObservabel() {
        return Observable.create(aSubscribe -> {
            for(int i=0;i<50;i++) {
                aSubscribe.onNext("NonBlockingValue:" + i);
            }
        });
    }
    public static void main(String[] args) {
        asyncObservable().subscribeOn(Schedulers.io()).subscribe(System.out::println);
        asyncNonblockingObservabel().subscribeOn(Schedulers.io()).subscribe(System.out::println);
        //main方法容易在线程还没有切换的时候结束，然后就看不到结果了。这个地方卡了我好久啊
        try {
            Thread.sleep(1000*2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
